package com.rayboot.footpoint.activity;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.google.android.maps.MapView;
import com.rayboot.footpoint.R;
import com.rayboot.footpoint.base.BaseActivity;
import com.rayboot.footpoint.db.FootpointManager;
import com.rayboot.footpoint.model.Category;
import com.rayboot.footpoint.model.Footprint;
import com.rayboot.footpoint.utils.FootpointConstant;
import com.rayboot.footpoint.utils.Util;
import com.rayboot.footpoint.view.MediaControlPanelEditMode;

public class FootpointDetailViewActivity extends BaseActivity {

	private Footprint mFootprint;
	TextView tvTitle;
	ImageView ivPhoto;
	RatingBar rbStar;
	TextView tvCategory;
	ImageView ivCategory;
	TextView tvDate;
	TextView tvTime;
	TextView tvAddress;
	TextView tvPhone;
	TextView tvSite;
	MediaControlPanelEditMode mcRecord;
	TextView tvLat;
	TextView tvLon;
//	MapView mapView;
	TextView tvNotes;
	ImageView ivAddress;
	ImageView ivPhone;
	ImageView ivSite;
	ImageView ivLocation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_view);

		mFootprint = getIntent().getExtras().getParcelable(
				FootpointConstant.EXTRA_FOOTPOINT);

		ivPhoto = (ImageView) findViewById(R.id.ivPhoto);
		tvTitle = (TextView) findViewById(R.id.tvTitle);
		tvCategory = (TextView) findViewById(R.id.tvCategory);
		tvDate = (TextView) findViewById(R.id.tvDate);
		tvTime = (TextView) findViewById(R.id.tvTime);
		tvAddress = (TextView) findViewById(R.id.tvAddress);
		tvPhone = (TextView) findViewById(R.id.tvPhone);
		tvSite = (TextView) findViewById(R.id.tvSite);
		tvLat = (TextView) findViewById(R.id.tvLat);
		tvLon = (TextView) findViewById(R.id.tvLon);
		tvNotes = (TextView) findViewById(R.id.tvNotes);
		ivPhone = (ImageView) findViewById(R.id.ivPhoto);
		ivCategory = (ImageView) findViewById(R.id.ivCategory);
		ivAddress = (ImageView) findViewById(R.id.ivAddress);
		ivPhone = (ImageView) findViewById(R.id.ivPhone);
		ivSite = (ImageView) findViewById(R.id.ivSite);
		ivLocation = (ImageView) findViewById(R.id.ivLocation);
//		mapView = (MapView) findViewById(R.id.mvMap);
		rbStar = (RatingBar) findViewById(R.id.rbStar);
		mcRecord = (MediaControlPanelEditMode) findViewById(R.id.mcRecord);

		initContent(mFootprint);
	}

	private void initContent(Footprint footprint) {
		// TODO Auto-generated method stub
		if (null == footprint) {
			finish();
			return;
		}

		ivPhoto.setBackgroundDrawable(Drawable
				.createFromPath(footprint.mPicPath));
		SimpleDateFormat formatter = new SimpleDateFormat(
				getString(R.string.time_format));
		Date curDate = new Date(mFootprint.mCreateTime);
		tvDate.setText(formatter.format(curDate));
		tvCategory.setText(mFootprint.mCategory);
		ivCategory.setImageResource(Category.getCategoryIconResId(this,
				mFootprint.mCategory));
		
		footprint.mComment = "1111111111111";

		showAddressPanel(!TextUtils.isEmpty(footprint.mAddr));
		showphonePanel(!TextUtils.isEmpty(footprint.mTel));
		showWebPanel(!TextUtils.isEmpty(footprint.mSite));
		showMediaPanel(!Util.checkAudioData(footprint));
		showMapPanel(footprint.mAccuracy <= 0);
		showNotesPanel(!TextUtils.isEmpty(footprint.mComment));
	}

	private void showNotesPanel(boolean bShow) {
		// TODO Auto-generated method stub
		findViewById(R.id.flNote).setVisibility(
				bShow ? View.VISIBLE : View.GONE);
		if (bShow) {
			tvNotes.setText(mFootprint.mComment);
		}
	}

	private void showMapPanel(boolean bShow) {
		// TODO Auto-generated method stub
		findViewById(R.id.flMap)
				.setVisibility(bShow ? View.VISIBLE : View.GONE);
		if (bShow) {

		}
	}

	private void showMediaPanel(boolean bShow) {
		// TODO Auto-generated method stub
		findViewById(R.id.mcRecord).setVisibility(
				bShow ? View.VISIBLE : View.GONE);
		findViewById(R.id.media_div).setVisibility(
				bShow ? View.VISIBLE : View.GONE);
		if (bShow) {

		}
	}

	private void showWebPanel(boolean bShow) {
		// TODO Auto-generated method stub
		findViewById(R.id.llSite).setVisibility(
				bShow ? View.VISIBLE : View.GONE);
		findViewById(R.id.web_div).setVisibility(
				bShow ? View.VISIBLE : View.GONE);
		if (bShow) {
			tvSite.setText(mFootprint.mSite);
		}
	}

	private void showphonePanel(boolean bShow) {
		// TODO Auto-generated method stub
		findViewById(R.id.llPhone).setVisibility(
				bShow ? View.VISIBLE : View.GONE);
		findViewById(R.id.phone_div).setVisibility(
				bShow ? View.VISIBLE : View.GONE);
		if (bShow) {
			tvPhone.setText(mFootprint.mTel);
		}
	}

	private void showAddressPanel(boolean bShow) {
		// TODO Auto-generated method stub
		findViewById(R.id.llAddress).setVisibility(
				bShow ? View.VISIBLE : View.GONE);
		findViewById(R.id.address_div).setVisibility(
				bShow ? View.VISIBLE : View.GONE);
		if (bShow) {
			tvAddress.setText(mFootprint.mAddr);
		}
	}

}
